package com.jiepos.mpos.biz.code.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jiepos.mpos.biz.code.service.MenuButtonService;
import com.jiepos.mpos.biz.code.service.MenuCodeService;
import com.jiepos.mpos.common.entity.App_MenuButton_InfEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;

/**
 * 菜单功能维护控制层
 * @ClassName: MenuButtonController
 * @Description: 菜单功能维护控制层
 * @author 孙永
 * @date 2017年9月13日 上午11:44:20
 *
 */
@Controller
@RequestMapping("menubtn")
public class MenuButtonController extends BaseController {
	
	@Autowired
	private MenuCodeService menuCodeService;
	@Autowired
	private MenuButtonService menuButtonService;

	/**
	 * 跳转列表页面
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	@RequestMapping("/getMenubtnList.do")
	public ModelAndView getMenubtnList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView("/module/code/menubutton/menubutton_list");
		return super.getDetail(request, response, model);
	}
	
	@RequestMapping("/getMenubtnListJson.do")
	@ResponseBody
	public Map<String, Object> getMenubtnListJson(HttpServletRequest request, HttpServletResponse response, Pagination pagination, String muuid){
		return menuButtonService.getMenuBtnPageList(pagination, muuid);
	}
	
	/**
	 * 获取系统菜单树
	 * @return
	 */
	@RequestMapping("/getMenuTree.do")
	@ResponseBody
	public List<Map<String, Object>> getMenuTree(){
		return menuCodeService.getMenuTree(true);
	}
	
	/**
	 * 菜单按钮详情页面
	 * @param request
	 * @param response
	 * @param mUuid 菜单编码
	 * @param button_uuid 按钮编码
	 * @return
	 */
	@RequestMapping("/getMenubtnDetail.do")
	public ModelAndView getMenubtnDetail(HttpServletRequest request, HttpServletResponse response, String muuid, String button_uuid) {
		ModelAndView model = new ModelAndView("/module/code/menubutton/menubutton_detail");
		model.addObject("menu_uuid", muuid); // 菜单编码
		model.addObject("btnEntity", menuButtonService.getMenuBtnByUuid(button_uuid));
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 保存菜单按钮关系
	 * @param request
	 * @param response
	 * @param btnEntity
	 * @return
	 */
	@RequestMapping("/saveBtn.do")
	@ResponseBody
	public Map<String, Object> saveMenu(HttpServletRequest request, HttpServletResponse response, App_MenuButton_InfEntity btnEntity) {
		Map<String, Object> resp = new HashMap<>();
		resp.put("isOk", false);
		try{
			menuButtonService.saveMenu(btnEntity, super.getLoginUser(request, response));
			resp.put("isOk", true);
		}catch(Exception e){
			e.printStackTrace();
		}
		return resp;
	}
	
	/**
	 * 删除菜单按钮
	 * @param request
	 * @param response
	 * @param button_uuids 按钮代码
	 * @return
	 */
	@RequestMapping("/deleteMenuBtnByIds.do")
	public ModelAndView deleteMenuBtnByIds(HttpServletRequest request, HttpServletResponse response, String button_uuids) {
		menuButtonService.deleteMenuBtnByIds(button_uuids);
		Map map=super.getLoginUser(request, response);  
		return new ModelAndView(new RedirectView("getMenubtnList.do"), map);
	}
}
